package com.xcity.game.achievement.helper;

import com.xcity.db.entity.Player;
import com.xcity.game.achievement.Achievement;
import com.xcity.game.achievement.Achievement.State;
import com.xcity.game.achievement.AchievementHelper;
import com.xcity.game.achievement.AchievementTemplate;
import com.xcity.game.buildv2.BuildV2;

/**
 * 成就类型：拥有（已建成的）建筑数量<br/>
 * 1个参数：①完成该成就需拥有的建筑总数
 * @author yang.li
 *
 */
public class BuildingsNumHelper implements AchievementHelper {

	@Override
	public boolean update(Achievement refer, int... vals) {
		return isFinished(refer);
	}

	@Override
	public boolean isFinished(Achievement refer) {
		if (refer.getState() == State.INITED
				&& refer.getVm().getOwner().getBuildings2().size() >= obtainCount(refer.getTemplate())) {
			Player player = refer.getVm().getOwner();
			int count = player.getBuildings2().size();
			BuildV2 last = player.getBuildings2().getLast();
			if (last.getState() != com.xcity.game.buildv2.BuildV2.State.BUILT
					&& last.getState() != com.xcity.game.buildv2.BuildV2.State.CAPTURED) {
				// 最后一个未建成
				--count;
			}
			if (count >= obtainCount(refer.getTemplate())) {
				refer.setState(State.FINISHED);
				return true;
			}
		}
		return false;
	}
	
	static int obtainCount(AchievementTemplate template) {
		return (int) template.getCondition(0);
	}
	
}
